package net.javlov.example.landmarks;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import net.javlov.world.Body;
import net.javlov.world.grid.GridCell;
import net.javlov.world.grid.IGridWorld;
import net.javlov.world.ui.GridWorldView;

public class LandmarkGridWorldView extends GridWorldView {

	List<Point> landmarkBorder;
	
	public LandmarkGridWorldView(IGridWorld w, int dist) {
		super(w);
		
		landmarkBorder = new ArrayList<Point>(4*dist);
		int y = 0;
		for ( int x=-dist; x<=0; x++) {
			landmarkBorder.add( new Point(x,y));
			if ( x != 0 )
				landmarkBorder.add( new Point(-x,y));

			if ( y != 0 )
				landmarkBorder.add( new Point(x,-y));

			if ( y != 0 && x != 0 )
				landmarkBorder.add( new Point(-x,-y));

			y -= 1;

		}
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.transform(getScaleTransform());
		GridCell[][] cells = ((IGridWorld) model).getGrid().getCells();
		g2d.setColor(Color.lightGray);
		for(int h = 0; h < cells[0].length; h++) {
    		for(int w = 0; w < cells.length; w++) {
    			g2d.draw(cells[w][h]);
    		}
		}
		
		Color[] colors = new Color[4];
		colors[0] = Color.red;
		colors[1] = Color.green; 
		colors[2] = Color.blue; 
		colors[3] = Color.orange; 
		int c = 0;
		int lx, ly;
		for(int h = 0; h < cells[0].length; h++) {
    		for(int w = 0; w < cells.length; w++) {

    			List<Body> occupiers = cells[w][h].getOccupiers();
    			for ( int i = 0; i < occupiers.size(); i++ ) {
    			  if (occupiers.get(i).getType() == Body.UNDEFINED) {
    				  g2d.setColor(colors[c++]);
					for ( Point p : landmarkBorder ) {
						lx = p.x+w;
						ly = p.y+h;
						if ( lx < cells.length && lx >= 0 && ly < cells[0].length && ly >= 0 )
					  			g2d.draw(cells[lx][ly]);
					}
    			  }
    			}
    			
    		}
		}
		
		

		g2d.setTransform(getOriginalTransform());
	}

}
